import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '工作台', icon: 'dashboard' }
    }]
  },
  // 基础数据管理
  {
    path: '/datas',
    component: Layout,
    name: 'branches',
    meta: { title: '基础数据管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'freight-manage',
        name: 'FreightManage',
        component: () => import('@/views/datas/FreightManage'),
        meta: { title: '运费管理' }
      },
      {
        path: 'jobs-area',
        name: 'JobsArea',
        component: () => import('@/views/datas/JobsArea'),
        meta: { title: '机构作业范围' }
      },
      {
        path: 'organization-manage',
        name: 'OrganizationManage',
        component: () => import('@/views/datas/OrganizationManage'),
        meta: { title: '机构管理' }
      }
    ]
  },
  // 车辆管理
  {
    path: '/car',
    component: Layout,
    name: 'car',
    meta: { title: '车辆管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'car-moudels',
        name: 'CarMoudels',
        component: () => import('@/views/car/CarModels'),
        meta: { title: '车型管理' }
      },
      {
        path: 'car-list',
        name: 'CarList',
        component: () => import('@/views/car/CarList'),
        meta: { title: '车辆列表' }
      },
      {
        path: 'car-rgiser',
        name: 'CarRgiser',
        component: () => import('@/views/car/CarRegister'),
        meta: { title: '回车登记' }
      }
    ]
  },
  // 员工管理
  {
    path: '/employees',
    component: Layout,
    name: 'employees',
    meta: { title: '员工管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'courier-manage',
        name: 'CourierManage',
        component: () => import('@/views/employees/CourierManage'),
        meta: { title: '快递员管理' }
      },
      {
        path: 'driver-manag',
        name: 'DriverManage',
        component: () => import('@/views/employees/DriverManage'),
        meta: { title: '司机管理' }
      },
      {
        path: 'arrange-manage',
        name: 'ArrangeManage',
        component: () => import('@/views/employees/ArrangeManage'),
        meta: { title: '排班管理' }
      }
    ]
  },
  // 业务管理
  {
    path: '/business',
    component: Layout,
    name: 'business',
    meta: { title: '业务管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'order-manage',
        name: 'OrderManage',
        component: () => import('@/views/business/OrderManage'),
        meta: { title: '订单管理' }
      },
      {
        path: 'waybill-manag',
        name: 'WaybillManage',
        component: () => import('@/views/business/WaybillManage'),
        meta: { title: '运单管理' }
      }
    ]
  },
  // 调度管理
  {
    path: '/dispatch',
    component: Layout,
    name: 'dispatch',
    meta: { title: '调度管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'task-manage',
        name: 'TaskManage',
        component: () => import('@/views/dispatch/TaskManage'),
        meta: { title: '运输任务管理' }
      },
      {
        path: 'line-manag',
        name: 'LineManage',
        component: () => import('@/views/dispatch/LineManage'),
        meta: { title: '线路管理' }
      },
      {
        path: 'pick-manage',
        name: 'PickManage',
        component: () => import('@/views/dispatch/PickManage'),
        meta: { title: '取件作业管理' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
